//
//  WBBaseViewController.swift
//  新浪微博
//
//  Created by 陈飞宇 on 2020/9/24.
//

import UIKit




class WBBaseViewController: UIViewController {
        

    var userLogin = false
    //访客视图信息字典
    var visitorInfo : [String : String]?
    
    var tableView : UITableView?
//    自定义导航条 LayoutNavigationBar重写父类方法  用UI的话 会少一截
    lazy var navigationBar = LayoutNavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height:is_X_Seris ? 88 : 64 ));
//    自定义导航项
    lazy var navItem  = UINavigationItem()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        setUpUI()
        loadData()
    }
    
    //重写title
    override var title: String?{
        didSet{
            navItem.title = title
        }
    }
    
    
    //加载数据 由子类负责
    @objc func loadData(){
        tableView?.mj_footer?.endRefreshing();

    }
    
    //加载数据 由子类负责
    @objc func loadMoreData(){
        print("!!!!!");
        tableView?.mj_header?.endRefreshing();
    }
    
    
}
extension WBBaseViewController
{
    @objc func setUpUI(){
        view.backgroundColor = UIColor.white
        setUpNav();

        userLogin ? setUpTableView() : setUpVisitorView()

        //如果登录 设置表格 如果没登录 设置访客模式

    }
    
    
    func  setUpVisitorView() {//设置访客视图
        
        let visitorView =  WBVistorView(frame: view.bounds);
//        view.addSubview(visitorView)
        view.insertSubview(visitorView, belowSubview: navigationBar)
        visitorView.visitorInfo = visitorInfo
        
    }
    
    func setUpTableView()  {
        tableView = UITableView(frame: view.bounds, style: .plain)
        view .insertSubview(tableView!, belowSubview: navigationBar)
        if #available(iOS 13.0, *) {
            tableView?.contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false;
        };
        
    
        tableView?.dataSource = self;
        tableView?.delegate = self;
                
        tableView?.contentInset = UIEdgeInsets(top: navigationBar.bounds.height, left: 0, bottom: tabBarController?.tabBar.bounds.height ?? 49, right: 0)
        
        
        let header = MJRefreshNormalHeader();
        header.setRefreshingTarget(self, refreshingAction: #selector(loadData));
        tableView?.mj_header = header;
        
        let footer = MJRefreshBackNormalFooter();
        footer.setRefreshingTarget(self, refreshingAction: #selector(loadMoreData))
        tableView?.mj_footer = footer;

        
    }
      
    func setUpNav() {
        //添加导航条
        view.addSubview(navigationBar);
        //将item 设置给bar
        navigationBar.items = [navItem];
        navigationBar.barTintColor = UIColor.cz_color(withHex: 0xF6F6F6);

        navigationBar.titleTextAttributes = [
            NSAttributedString.Key.foregroundColor: UIColor.darkGray,
        ];
    }
    
    
}

extension WBBaseViewController : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0;
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell();
    }
}
